<?php
/**
 * Created by PhpStorm.
 * User: WX-C
 * Date: 2018/4/19
 * Time: 15:41
 */

namespace app\common\model;


use app\common\traits\Base;
use think\Db;
use think\Model;
use think\Request;
use think\View;

class Questionnaire extends Model
{
    protected function setLockAttr($value){
        return $value=='true'?1:0;
    }
    protected function getCategoryAttr($value){
        return json_decode($value,true);
    }

    public function index(){
        $request=Request::instance();
        $limit=$request->param('limit',10,'intval');
        $page=$request->param('page',0,'intval');
        $name=$request->param('name','');
        $where=[];
        if($name){
            $where=['name'=>['like',"%{$name}%"]];
        }
        $lists=$this
            ->page($page,$limit)
            ->where($where)
            ->select();
        return $lists;
    }

    public function getQidsAttr($value){
        return array_filter(explode(',',$value));
    }
    public function question(){
        $model=new Question();
        $questions=$model->where(['id'=>['in',$this->qids]])
            ->column('type,name,ext','id');
        return $questions;
    }
    public function add($data){
        $r=$this->allowField(true)
            ->isUpdate(false)
            ->save($data);
    }
    public function edit($data){
        $r=$this->allowField(true)
            ->isUpdate(true)
            ->save($data);
    }

    public function idFind($id){
        return $lists=$this->find($id);
    }

    public function del($id){
        return $lists=$this->where(['id'=>$id])->delete();
    }


    public function idQuestion($qids){
        $where['qid']=array('in',$qids);
       $question=Db::table('question')
            ->order('weight')
            ->where($where)
            ->select();
        return $question;
    }

    public function idQuestions($id){
        $where['qid']=array('in',$id);
        $question=Db::table('question')
            ->order('weight')
            ->where($where)
            ->select();
        return $question;
    }

    public function cate_add($data){
        $tmp=['category'=>$data['data'],'name'=>$data['title']];
        $r=$this->isUpdate(false)
            ->save($tmp);
        return $r;
    }
    public function cate_edit($data){
        $tmp=['category'=>$data['data'],'name'=>$data['title'],'id'=>$data['id']];
        $r=$this
            ->isUpdate(true)
            ->save($tmp);
        return $r;
    }
    public function answer(){
        return $this->hasMany('answer','naire','id');
    }
    public function cate_sort($data){
        return $this->edit($data);
    }
    public function tongjibak($id)
    {
        $model = $this->where(['id' => $id])
            ->find();
        $questions = $model->question();
        $base = [];
        foreach ($questions as $question) {
            $answer_count = [];
            $question['ext'] = json_decode($question['ext'], true);
            switch ($question['type']) {
                case 'radio':
                case 'checkbox':
                    foreach ($question['ext'] as $e) {
                        $answer_count[$e['name']] = 0;
                    }
                    break;
                case 'table':
                    foreach ($question['ext']['td'] as $td) {
                        $answer_count[$td] = array_combine($question['ext']['th']
                            , array_fill(0, count($question['ext']['th']), 0));
                    }
                    break;
            }
            if(count($answer_count)){
                $base[$question['id']] = [
                    'type' => $question['type'],
                    'name' => $question['name'],
                    'answer' => $answer_count
                ];
            }
        }
        $answers = $model->answer;
        $group=[];
        foreach ($answers as $answer) {
            $answer['long_json']=array_combine(array_column($answer['long_json'],'id'),$answer['long_json']);
            if(!isset($group[$answer['parent']][$answer['children']])){
                $group[$answer['parent']][$answer['children']]=$base;
            }
            $tmp=&$group[$answer['parent']][$answer['children']];

            foreach ($answer['long_json'] as $l) {
                if(isset($tmp[$l['id']])){
                    switch ($tmp[$l['id']]['type']) {
                        case 'radio':
                            $tmp[$l['id']]['answer'][$l['name']]++;
                            break;
                        case 'checkbox':
                            foreach ($l['name'] as $name) {
                                $tmp[$l['id']]['answer'][$name]++;
                            }
                            break;
                        case 'table':
                            foreach ($l['name'] as $key => $name) {
                                $tmp[$l['id']]['answer'][$key][$name]++;
                            }
                            break;
                    }
                }
            }
        }
        return [$group,$base];
    }


    public function tongji($id)
    {
        $model = $this->where(['id' => $id])
            ->find();
        $questions = $model->question();
        $base = [];
        foreach ($questions as $question) {
            $answer_count = [];
            $question['ext'] = json_decode($question['ext'], true);
            switch ($question['type']) {
                case 'radio':
                case 'checkbox':
                    foreach ($question['ext'] as $e) {
                        $answer_count[$e['name']] = 0;
                    }
                    break;
                case 'table':
                    //矩阵统计选项
                    foreach ($question['ext']['td'] as $td) {
                        $tmp=array_map(function($v){
                            list($a,$b)=explode(',',$v);
                            return $v;
                        },$question['ext']['th']);
                        $answer_count[$td] = array_combine(
                            $tmp, array_fill(0, count($question['ext']['th']), 0));
                    }
                    break;
            }
            if(count($answer_count)){
                $base[$question['id']] = [
                    'type' => $question['type'],
                    'name' => $question['name'],
                    'answer' => $answer_count
                ];
            }
        }
        $answers = $model->answer;
        $group=[];
        foreach ($answers as $answer) {
            $answer['long_json']=array_combine(array_column($answer['long_json'],'id'),$answer['long_json']);
            if(!isset($group[$answer['parent']])){
                $group[$answer['parent']]=$base;
            }
            $tmp=&$group[$answer['parent']];

            foreach ($answer['long_json'] as $l) {
                if(isset($tmp[$l['id']])){
                    switch ($tmp[$l['id']]['type']) {
                        case 'radio':
                            $tmp[$l['id']]['answer'][$l['name']]++;
                            break;
                        case 'checkbox':
                            foreach ($l['name'] as $name) {
                                $tmp[$l['id']]['answer'][$name]++;
                            }
                            break;
                        case 'table':
                            foreach ($l['name'] as $key => $name) {
                                $tmp[$l['id']]['answer'][$key][$name]++;
                            }
                            break;
                    }
                }
            }
        }
        return [$group,$base];
    }
    public function parentShow($id,$parent){
        $model = $this
            ->with(['answer'=>function($query)use($parent){
                $query->where(['parent'=>$parent]);
            }])
            ->find($id);
        $questions = $model->question();
        $base = [];
        foreach ($questions as $question) {
            $question['ext'] = json_decode($question['ext'], true);
            $answer_count = [];
            switch ($question['type']) {
                case 'radio':
                case 'checkbox':
                    foreach ($question['ext'] as $e) {
                        $answer_count[$e['name']] = 0;
                    }
                    break;
                case 'table':
                    foreach ($question['ext']['td'] as $td) {
                        $answer_count[$td] = array_combine($question['ext']['th']
                            , array_fill(0, count($question['ext']['th']), 0));
                    }
                    break;
                case 'textarea':
                case 'text':
                    $answer_count[$question['name']]='';
                    break;
            }
            $base[$question['id']] = [
                'type' => $question['type'],
                'name' => $question['name'],
                'answer' => $answer_count
            ];
        }
        $answers = $model->answer;
        $group=[];
        foreach ($answers as $answer) {
            $answer['long_json']=array_combine(array_column($answer['long_json'],'id'),$answer['long_json']);
            if(!isset($group[$answer['parent']][$answer['children']])){
                $group[$answer['parent']][$answer['children']]=$base;
            }
            $tmp=&$group[$answer['parent']][$answer['children']];

            foreach ($answer['long_json'] as $l) {
                if(isset($tmp[$l['id']])){
                    switch ($tmp[$l['id']]['type']) {
                        case 'radio':
                            $tmp[$l['id']]['answer'][$l['name']]++;
                            break;
                        case 'checkbox':
                            foreach ($l['name'] as $name) {
                                $tmp[$l['id']]['answer'][$name]++;
                            }
                            break;
                        case 'table':
                            foreach ($l['name'] as $key => $name) {
                                $tmp[$l['id']]['answer'][$key][$name]++;
                            }
                            break;
                    }
                }
            }
        }
        return [$group,$base];
    }
    public function childrenShow($id,$parent,$children)
    {
        $model = $this
            ->with(['answer'=>function($query)use($parent,$children){
                $query->where(['parent'=>$parent,'children'=>$children]);
            }])
            ->find($id);
        $questions = $model->question();
        $base = [];
        foreach ($questions as $question) {
            $question['ext'] = json_decode($question['ext'], true);
            $answer_count = [];
            switch ($question['type']) {
                case 'radio':
                case 'checkbox':
                    foreach ($question['ext'] as $e) {
                        $answer_count[$e['name']] = 0;
                    }
                    break;
                case 'table':
                    foreach ($question['ext']['td'] as $td) {
                        $answer_count[$td] = array_combine($question['ext']['th']
                            , array_fill(0, count($question['ext']['th']), 0));
                    }
                    break;
                case 'textarea':
                case 'text':
                $answer_count[$question['name']]='';
                    break;
            }
            $base[$question['id']] = [
                'type' => $question['type'],
                'name' => $question['name'],
                'answer' => $answer_count
            ];
        }
        $answers = $model->answer;
        $group=[];
        foreach ($answers as $answer) {
            $tmp=$base;
            $answer['long_json']=array_combine(array_column($answer['long_json'],'id'),$answer['long_json']);
            foreach ($answer['long_json'] as $l) {
                switch ($tmp[$l['id']]['type']) {
                    case 'radio':
                        $tmp[$l['id']]['answer'][$l['name']]++;
                        break;
                    case 'checkbox':
                        foreach ($l['name'] as $name) {
                            $tmp[$l['id']]['answer'][$name]++;
                        }
                        break;
                    case 'table':
                        foreach ($l['name'] as $key => $name) {
                            $tmp[$l['id']]['answer'][$key][$name]++;
                        }
                        break;
                    case 'text':
                    case 'textarea':
                        $tmp[$l['id']]['answer'][$questions[$l['id']]['name']]=$l['name'];
                }
            }
            $group[]=$tmp;
        }

        return [$group,$base];
    }

    //只统计矩阵
    public function juzhengShow($id,$parent,$children)
    {
        $model = $this
            ->with(['answer'=>function($query)use($parent,$children){
                $query->where(['parent'=>$parent,'children'=>$children]);
            }])
            ->find($id);
        $questions = $model->question();
        $base = [];
        foreach ($questions as $question) {
            $question['ext'] = json_decode($question['ext'], true);
            $answer_count = [];
            switch ($question['type']) {
                case 'table':
                    foreach ($question['ext']['td'] as $td) {
                        $answer_count[$td] = array_combine($question['ext']['th']
                            , array_fill(0, count($question['ext']['th']), 0));
                    }
                    break;
                default:
                    continue;
            }
            if(count($answer_count)){
                $base[$question['id']] = [
                    'type' => $question['type'],
                    'name' => $question['name'],
                    'answer' => $answer_count
                ];
            }
        }
        $answers = $model->answer;
        $group=$base;
        foreach ($answers as $answer) {
            $answer['long_json']=array_combine(array_column($answer['long_json'],'id'),$answer['long_json']);
            foreach ($answer['long_json'] as $l) {
                if(!isset($group[$l['id']])){
                    continue;
                }
                switch ($group[$l['id']]['type']) {
                    case 'table':
                        foreach ($l['name'] as $key => $name) {
                            $group[$l['id']]['answer'][$key][$name]++;
                        }
                        break;
                }
            }
        }
        return [$group,$base];
    }
}